package com.example.databasedemo.slice;

import com.example.databasedemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;

import java.util.List;
import java.util.function.Consumer;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Text text = findComponentById(ResourceTable.Id_text_helloworld);
//        // 1. 打开数据库
//        DatabaseHelper helper = new DatabaseHelper(getContext());
//        StoreConfig config = StoreConfig.newDefaultConfig("MyDB.db");
//
//        RdbOpenCallback callback = new RdbOpenCallback() {
//            @Override
//            public void onCreate(RdbStore rdbStore) {
//                rdbStore.executeSql("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER, subject TEXT)");
//            }
//
//            @Override
//            public void onUpgrade(RdbStore rdbStore, int currentVersion, int targetVersion) {
//
//            }
//        };

//        RdbStore rdbStore = helper.getRdbStore(config, 1, callback, null);

//        // 2. 存储数据
//        ValuesBucket valuesBucket = new ValuesBucket();
//        valuesBucket.putString("name", "huishao");
//        valuesBucket.putString("subject", "Harmony OS");
//        valuesBucket.putInteger("age", 30);
//        rdbStore.insert("student", valuesBucket);
//
//        valuesBucket.putString("name", "huishao");
//        valuesBucket.putString("subject", "Java");
//        valuesBucket.putInteger("age", 29);
//        rdbStore.insert("student", valuesBucket);
//        // 更新数据
//        valuesBucket.putString("name", "Even");
//        valuesBucket.putInteger("id", 1);
//        rdbStore.replace("student", valuesBucket);
//        valuesBucket.putString("name", "Even");
//        rdbStore.beginTransaction();
//        rdbStore.update(valuesBucket, new RdbPredicates("student").equalTo("name", "huishao"));
//        // 删除数据
//        rdbStore.delete(new RdbPredicates("student").equalTo("age", 30));
//        rdbStore.endTransaction();



//        // 3. 查询数据
//        String[] columns = new String[] {"id", "name", "age", "subject"};
//        RdbPredicates rdbPredicates = new RdbPredicates("student").orderByAsc("age");
//        ResultSet resultSet = rdbStore.query(rdbPredicates, columns);
//        String string = "";
//        while (resultSet.goToNextRow()) {
//            int id = resultSet.getInt(0);
//            String name = resultSet.getString(1);
//            int age = resultSet.getInt(2);
//            String subject = resultSet.getString(3);
//            string += String.format("id:%d\nname:%s\nage:%d\nsubject:%s\n\n", id, name, age, subject);
//        }
//        text.setText(string);
//        rdbStore.close();

        // ORM相关
        DatabaseHelper helper = new DatabaseHelper(this);
        OrmContext ormContext = helper.getOrmContext("MyDB", "MyDB.db", MyDB.class);
        Student student1 = new Student();
        Student student2 = new Student();
        student1.setName("huishao");
        student1.setAge(30);
        student1.setSubject("Harmony OS");
        student1.setId(0);
        student2.setName("Even");
        student2.setAge(29);
        student2.setSubject("Java");
        student2.setId(1);
        student2.setClassroom("302");
        // 插入数据
        ormContext.insert(student1);
        ormContext.flush();
        ormContext.insert(student2);
        ormContext.flush();

        OrmPredicates predicates = ormContext.where(Student.class);
        List<Student> studentList = ormContext.query(predicates);
        String string = "";
        for (int i = 0; i < studentList.size(); i++) {
            Student student = studentList.get(i);
            string += String.format("id:%d\nname:%s\nage:%d\nsubject:%s\nclassroom:%s\n\n", student.getId(), student.getName(), student.getAge(), student.getSubject(), student.getClassroom());
        }

        Double max = ormContext.max(predicates, "age");
        Double min = ormContext.min(predicates, "age");
        Double sum = ormContext.sum(predicates, "age");
        Double avg = ormContext.avg(predicates, "age");
        string += String.format("\n年龄：最大%.2f,最小%.2f,平均%.2f,总和:%.2f", max, min, avg, sum);

        text.setText(string);
        ormContext.close();
    }

}
